<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: MySpace::EndPoint</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">MySpace::EndPoint</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/lib/myspace/end_point_rb.html">
                lib/myspace/end_point.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                Object
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000060">compute_path</a>&nbsp;&nbsp;
      <a href="#M000056">define</a>&nbsp;&nbsp;
      <a href="#M000057">find</a>&nbsp;&nbsp;
      <a href="#M000058">new</a>&nbsp;&nbsp;
      <a href="#M000059">parse_path</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





    <div id="attribute-list">
      <h3 class="section-bar">Attributes</h3>

      <div class="name-list">
        <table>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">method</td>
          <td class="context-item-value">&nbsp;[R]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        </table>
      </div>
    </div>
      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000056" class="method-detail">
        <a name="M000056"></a>

        <div class="method-heading">
          <a href="#M000056" class="method-signature">
          <span class="method-name">define</span><span class="method-args">(name, path, method, *options)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Registers a <a href="EndPoint.html#M000058">new</a> <a
href="EndPoint.html">EndPoint</a> named <tt>name</tt>.
</p>
<p>
<tt>path</tt> is the path on server which may include variables in braces,
e.g.
</p>
<pre>
  '/v1/users/{user_id}/profile'
</pre>
<p>
In this case, when you call <tt><a
href="EndPoint.html#M000060">compute_path</a></tt> for this <tt><a
href="EndPoint.html">EndPoint</a></tt>, you must include <tt>user_id</tt>
in <tt>params</tt>, which will be substituted into the url.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000056-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000056-source">
<pre>
<span class="ruby-comment cmt"># File lib/myspace/end_point.rb, line 15</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">define</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">method</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">options</span>)
      <span class="ruby-ivar">@@registry</span>[<span class="ruby-identifier">name</span>] = <span class="ruby-constant">EndPoint</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">options</span>)
    <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000057" class="method-detail">
        <a name="M000057"></a>

        <div class="method-heading">
          <a href="#M000057" class="method-signature">
          <span class="method-name">find</span><span class="method-args">(name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the <a href="EndPoint.html">EndPoint</a> named <tt>name</tt>.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000057-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000057-source">
<pre>
<span class="ruby-comment cmt"># File lib/myspace/end_point.rb, line 20</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">name</span>)
      <span class="ruby-keyword kw">return</span> <span class="ruby-ivar">@@registry</span>[<span class="ruby-identifier">name</span>]
    <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000058" class="method-detail">
        <a name="M000058"></a>

        <div class="method-heading">
          <a href="#M000058" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(path, method = :get, options = [])</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000058-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000058-source">
<pre>
<span class="ruby-comment cmt"># File lib/myspace/end_point.rb, line 24</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">method</span> = <span class="ruby-identifier">:get</span>, <span class="ruby-identifier">options</span> = [])
      <span class="ruby-ivar">@components</span> = <span class="ruby-constant">EndPoint</span>.<span class="ruby-identifier">parse_path</span>(<span class="ruby-identifier">path</span>)
      <span class="ruby-ivar">@method</span> = <span class="ruby-identifier">method</span>
      <span class="ruby-ivar">@options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">dup</span>
      <span class="ruby-ivar">@options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:v1_json</span>)
      <span class="ruby-ivar">@v1_json</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">detect</span>(<span class="ruby-identifier">:v1_json</span>)
    <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000059" class="method-detail">
        <a name="M000059"></a>

        <div class="method-heading">
          <a href="#M000059" class="method-signature">
          <span class="method-name">parse_path</span><span class="method-args">(path)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000059-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000059-source">
<pre>
<span class="ruby-comment cmt"># File lib/myspace/end_point.rb, line 32</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">parse_path</span>(<span class="ruby-identifier">path</span>)
      <span class="ruby-comment cmt"># parse the path string looking for {var} to substitute</span>
      <span class="ruby-identifier">fragments</span> = []
      <span class="ruby-identifier">pos</span> = <span class="ruby-value">0</span>
      <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">pos</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">length</span>
        <span class="ruby-identifier">lbrace</span> = <span class="ruby-identifier">path</span>.<span class="ruby-identifier">index</span>(<span class="ruby-value str">'{'</span>, <span class="ruby-identifier">pos</span>)
        <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">lbrace</span>
          <span class="ruby-identifier">fragments</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">path</span>[<span class="ruby-identifier">pos</span>, <span class="ruby-identifier">path</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">pos</span>])
          <span class="ruby-keyword kw">break</span>
        <span class="ruby-keyword kw">end</span>
        <span class="ruby-identifier">rbrace</span> = <span class="ruby-identifier">path</span>.<span class="ruby-identifier">index</span>(<span class="ruby-value str">'}'</span>, <span class="ruby-identifier">lbrace</span>)
        <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;unmatched left brace in '#{path}'&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">rbrace</span>
        <span class="ruby-identifier">fragments</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">path</span>[<span class="ruby-identifier">pos</span>, <span class="ruby-identifier">lbrace</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">pos</span>])
        <span class="ruby-identifier">var</span> = <span class="ruby-identifier">path</span>[<span class="ruby-identifier">lbrace</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>, <span class="ruby-identifier">rbrace</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">lbrace</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>].<span class="ruby-identifier">to_sym</span>
        <span class="ruby-identifier">fragments</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">var</span>)
        <span class="ruby-identifier">pos</span> = <span class="ruby-identifier">rbrace</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
      <span class="ruby-keyword kw">end</span>

      <span class="ruby-identifier">fragments</span>
    <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000060" class="method-detail">
        <a name="M000060"></a>

        <div class="method-heading">
          <a href="#M000060" class="method-signature">
          <span class="method-name">compute_path</span><span class="method-args">(params)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Computes the path to the <a href="EndPoint.html">EndPoint</a> by
substituting parameters from <tt>params</tt> into the path.
</p>
<p>
WARNING: This function DESTRUCTIVELY modifies the <tt>params</tt> hash that
you pass to it. This is normally what you want, since you don&#8216;t want
to substitute a parameter into the path and also pass it in the query
string, but make sure to make a copy first if you&#8216;re just passing in
parameters from another caller.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000060-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000060-source">
<pre>
<span class="ruby-comment cmt"># File lib/myspace/end_point.rb, line 61</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">compute_path</span>(<span class="ruby-identifier">params</span>)
      <span class="ruby-identifier">path</span> = <span class="ruby-ivar">@components</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-value str">&quot;&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">result</span>, <span class="ruby-identifier">cur</span><span class="ruby-operator">|</span>
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cur</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">String</span>
          <span class="ruby-identifier">result</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">cur</span>
        <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">cur</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Symbol</span>
          <span class="ruby-identifier">val</span> = <span class="ruby-identifier">params</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">cur</span>)
          <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Required parameter '#{cur}' omitted&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">val</span>
          <span class="ruby-identifier">result</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">val</span>
        <span class="ruby-keyword kw">else</span>
          <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Bad path component: #{cur}&quot;</span>
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">end</span>
      <span class="ruby-identifier">path</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value str">'.json'</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@v1_json</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:v1_json</span>]
      <span class="ruby-identifier">path</span>
    <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>